﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Zeus.Classes;
using Zeus.Classes.Documents;
using Zeus.Classes.Events;
using Zeus.CompilationService;

namespace Zeus
{
    public class Compiler
    {
        private ObservableCollection<string> ListFilesName;
        private ObservableCollection<string> Codes;
        private List<ErrorItem> errors;
        public Compiler()
        {
            ListFilesName = new ObservableCollection<string>();
            Codes = new ObservableCollection<string>();
            errors = new List<ErrorItem>(); 
        }
        private void GetCodes()
        {
            for (int i = 0; i < Project.ProjectDocuments.Count; i++)
            {
                TextDocument textDocument;
                if (Project.ProjectDocuments[i].DocType== ProjectItem_Types.Class)
                {
                    textDocument = (TextDocument) Project.ProjectDocuments[i];
                    Codes.Add(textDocument.ReadFile());
                    ListFilesName.Add(textDocument.FileName);
                }
            }
        }

        public void Compile()
        {
            GetCodes();
            CompilationWCFServiceClient comp = new CompilationWCFServiceClient();
            if(Project.ProjectType == ProjectTypeEnum.TowDimentions)
                comp.GetErrorsAsync(ListFilesName,Codes,true);
            else
                comp.GetErrorsAsync(ListFilesName, Codes, false);
            comp.GetErrorsCompleted += comp_GetErrorsCompleted;
        }

        void comp_GetErrorsCompleted(object sender, GetErrorsCompletedEventArgs e)
        {
            errors.AddRange(e.Result.ToList());
            MessageBox.Show(errors.Count.ToString());
            App.EventAggegator.GetEvent<FillErrorList>().Publish(errors);
        } 
    }
}
